/*
 * 文件名: YearMonthDayAmPmDialog.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2011-12-28
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import app.log.APPLog;
import app.utils.CommonUtils;
import app.widget.BaseDialog;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;
import app.widget.calendar.widget.adapter.DateNumericAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2011-12-28
 * @version [Android PABank C01, @2011-12-28]
 * @description 年月日 日历 Dialog 继承自BaseDialog
 */
public class YearMonthDayDialog extends BaseDialog implements OnWheelChangedListener,OnWheelScrollListener{
	/**
	 * 返回的日期结果
	 */
	private String result;
	/**
	 * 当前年
	 */
	private int curYear;
	/**
	 * 当前月
	 */
	private int curMonth;
	/**
	 * 当前天
	 */
	private int curDay;
	/**
	 * 年
	 */
	private int mYear;
	/**
	 * 月
	 */
	private int mMonth;
	/**
	 * 日
	 */
	private int mDay;
	/**
	 * 上下文
	 */
	private Context context;
	
	/**
	 * 最小日历
	 */
	private Calendar minTime;
	/**
	 * 最大日历
	 */
	private Calendar maxTime;
	/**
	 * 最小年
	 */
	private int minYear;
	/**
	 * 最大年
	 */
	private int maxYear;
	/**
	 * 最小月
	 */
	private int minMonth;
	/**
	 * 最大月
	 */
	private int maxMonth;
	/**
	 * 最小天
	 */
	private int minDay;
	/**
	 * 最大天
	 */
	private int maxDay;
	/**
	 * 月
	 */
	private WheelView month;
	/**
	 * 年
	 */
    private WheelView year;
    /**
     * 日
     */
    private WheelView day;
    /**
     * 返回View
     */
    private View contentView;
    /**
     * 是否能回弹
     */
    private boolean isScroll = false;
    
    private boolean isScrollBrith = false;
    
    /**
     * 不能小于今天
     */
    private boolean isScrollBeforeToday = false;
    
    
    
	public boolean isScrollBeforeToday() {
		return isScrollBeforeToday;
	}
	public void setScrollBeforeToday(boolean isScrollBeforeToday) {
		this.isScrollBeforeToday = isScrollBeforeToday;
	}
	public boolean isScrollBrith() {
		return isScrollBrith;
	}
	public void setScrollBrith(boolean isScrollBrith) {
		this.isScrollBrith = isScrollBrith;
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @return boolean
	 */
	public boolean isScroll() {
		return isScroll;
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param isScroll  是否能回弹
	 */

	public void setScroll(boolean isScroll) {
		this.isScroll = isScroll;
	}
	/**
	 *月日 回弹效果标志
	 */
	private boolean isScroll_month_day = false;
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @return boolean
	 */
	public boolean isScroll_month_day() {
		return isScroll_month_day;
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param isScroll_month_day 月日 回弹效果标志
	 */
	public void setScroll_month_day(boolean isScroll_month_day) {
		this.isScroll_month_day = isScroll_month_day;
	}
	/**
	 * 
	 * [构造简要说明]
	 * @param context 上下文
	 * @param title 标题
	 * @param minTime 最小日历
	 * @param maxTime 最大日历
	 */
	public YearMonthDayDialog(Context context, String title ,Calendar minTime,Calendar maxTime){
		super(context);
		this.context = context;
		this.minTime = minTime;
		this.maxTime = maxTime;
		setTitle(title);
		
		initDate();
		
	}
	
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化数据
	 */
	private void initDate() {
	
		initView();
		CalendarDialogCommon.addChangeListener(this, year , month ,day);
		
		setBtn1ClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				onDialogScrollEndListener.onDialogScrollEnd(result);
				YearMonthDayDialog.this.dismiss();
			}
		});
		
		setBtn2ClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				YearMonthDayDialog.this.dismiss();
			}
		});
		
		
		
		//Calendar calendar = Calendar.getInstance();
		Calendar calendar = CommonUtils.getCurrentTimeByCalendar();

        // month
        curMonth = calendar.get(Calendar.MONTH);
        
       
        month.setViewAdapter(new DateArrayAdapter(context, CalendarDialogCommon.MONTHS, curMonth));
        month.setCurrentItem(curMonth);
        
        mMonth =curMonth + 1;
    
        // year
        curYear = calendar.get(Calendar.YEAR);
        if(minTime != null){
        	minYear = minTime.get(Calendar.YEAR);
        	
        	minMonth = minTime.get(Calendar.MONTH) + 1;
        	minDay = minTime.get(Calendar.DAY_OF_MONTH);
        }else{
        	minYear = curYear;
        }
        
        if(maxTime != null){
        	maxYear = maxTime.get(Calendar.YEAR);
        	
        	maxMonth = maxTime.get(Calendar.MONTH) + 1;
        	maxDay = maxTime.get(Calendar.DAY_OF_MONTH);
        	//Log.i("zbkc", "maxYear:"+maxYear+"maxMonth:"+maxMonth+"maxDay:"+maxDay);
        }else{
        	maxYear = curYear + 100;
        }
        
        mYear = curYear;
        
        year.setViewAdapter(new DateNumericAdapter(context, minYear , maxYear, curYear-minYear,"年"));
        year.setCurrentItem(curYear-minYear);
        
        
        //day
        CalendarDialogCommon.updateDays(context,year, minYear,month, day);
        
 
        day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1);
       // Log.i("zbkc", "wai:"+ (calendar.get(Calendar.DAY_OF_MONTH)-1));
        curDay = calendar.get(Calendar.DAY_OF_MONTH);
        mDay = curDay;
        
        year.addScrollingListener(this);
        month.addScrollingListener(this);
        day.addScrollingListener(this);
        
        
        result = mYear + "-" + CalendarDialogCommon.convertZeroString(mMonth)+"-" + CalendarDialogCommon.convertZeroString(mDay) ;
        APPLog.i("zbkc", result);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化view
	 */
	private void initView(){
		contentView = LayoutInflater.from(context).inflate(R.layout.app_widget_year_month_day_layout, null);
		
		month = (WheelView) contentView.findViewById(R.id.month);
        year = (WheelView) contentView.findViewById(R.id.year);
        day = (WheelView) contentView.findViewById(R.id.day);
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 选择年月日
	 * @param y 年
	 * @param m 月
	 * @param d 日
	 */
	 public void select(int y, int m, int d) {

		 APPLog.i("zbkc", "year:"+y+"month:"+m+"day:"+d);
		 month.setCurrentItem(m-1);
		 year.setCurrentItem(y-minYear);
		 day.setCurrentItem(d - 1);
     
		 month.invalidate();
		 year.invalidate();
		 day.invalidate();
		 
   }
    /**
     * 监听
     */
    private OnDialogScrollEndListener onDialogScrollEndListener;
    /**
     * 
     * [一句话功能简述]<BR>
     * [功能详细描述] setter
     * @param onDialogScrollEndListener 监听
     */
    public void setOnDialogScrollEndListener(
			OnDialogScrollEndListener onDialogScrollEndListener) {
		this.onDialogScrollEndListener = onDialogScrollEndListener;
	}
    /**
     * 
     * [一句话功能简述]<BR>
     * [功能详细描述] getter
     * @return OnDialogScrollEndListener
     */
    public OnDialogScrollEndListener getOnDialogScrollEndListener() {
		return onDialogScrollEndListener;
	}


	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		APPLog.i("zbkc", "onChanged");
		switch (wheel.getId()) {
    	case R.id.year:
    		APPLog.i("zbkc", "minYear:"+minYear);
			mYear = minYear + newValue;
			
			break;
		case R.id.month:
			mMonth = newValue + 1;
			
			break;
		case R.id.day:
			mDay =  1 + newValue;
			
			break;
		
		default:
			break;
		}
    	CalendarDialogCommon.updateDays(context,year,minYear, month, day);
        
       
        result = mYear + "-" + CalendarDialogCommon.convertZeroString(mMonth)+"-" + CalendarDialogCommon.convertZeroString(mDay) ;
       // Log.i("zbkc", oldValue+"|"+newValue);
        //Log.i("zbkc", "zbkc:"+wheel.getCurrentItem());
        //Log.i("zbkc", "minYearffff:"+minYear);
        
        APPLog.i("zbkc", result);
	}

	private boolean scrollState = false;
	@Override
	public void onScrollingStarted(WheelView wheel) {
		// TODO Auto-generated method stub
		scrollState = true;
		if(onScrollEndListener!=null){
			onScrollEndListener.onScrollEnd(result,scrollState);
		}
		
		setBtn1Enable(false);
		setBtn2Enable(false);
	}


	@Override
	public void onScrollingFinished(WheelView wheel) {
		// TODO Auto-generated method stub
		APPLog.i("zbkc", "onScrollingFinished");
		scrollState = false;
		if(onScrollEndListener!=null){
			onScrollEndListener.onScrollEnd(result,scrollState);
		}
		
		
		
		if(isScroll){
			if(mYear>curYear){
				APPLog.i("zbkc", "yearmonthdialog:isScroll:"+isScroll+"mYear:"+mYear+"curYear:"+curYear);
				year.scroll(curYear-mYear, 0);
				year.invalidate();
			}
			if(mYear>=curYear&&mMonth>(curMonth+1)){
				APPLog.i("zbkc", "yearmonthdialog:isScroll:"+isScroll+"mMonth:"+mMonth+"curMonth:"+curMonth);
				month.scroll(curMonth-mMonth, 0);
				month.invalidate();
			}
			APPLog.i("zbkc", "yearmonthdialog:isScroll:"+isScroll+"mDay:"+mDay+"curDay:"+curDay);
			if(mYear>=curYear&&mMonth>=(curMonth+1)&&mDay>=(curDay+1)){
				
				day.scroll(curDay-mDay, 0);
				day.invalidate();
			}
		
		}
		
		if(isScrollBrith){
			APPLog.i("zbkc", "yearmonthdialog:isScrollxx:"+isScroll+"mYear:"+mYear+"curYear:"+curYear);
			if(mYear>curYear){
				
				year.scroll(curYear-mYear, 0);
				year.invalidate();
			}
			APPLog.i("zbkc", "yearmonthdialog:isScrollxx:"+isScroll+"mMonth:"+mMonth+"curMonth:"+curMonth);
			if(mYear>=curYear&&mMonth>(curMonth+1)){
				
				month.scroll(curMonth-mMonth+1, 0);
				month.invalidate();
			}
			APPLog.i("zbkc", "yearmonthdialog:isScrollxx:"+isScroll+"mDay:"+mDay+"curDay:"+curDay);
			if(mYear>=curYear&&mMonth>=(curMonth+1)&&mDay>=(curDay+1)){
				
				day.scroll(curDay-mDay, 0);
				day.invalidate();
			}
		}
		
		if(isScroll_month_day){
			
			
			if(mYear<minYear){
				year.scroll(minYear-mYear, 0);
			}
			
			APPLog.i("zbkc", "mMonth:"+mMonth+"curMonth:"+curMonth);
			if(mYear == minYear && mMonth< minMonth){
				month.scroll(minMonth-mMonth, 0);
				month.invalidate();
			}
			
			
			if(mYear == minYear&& mMonth== minMonth&&mDay< minDay){
				day.scroll(minDay-mDay, 0);
				day.invalidate();
			}
			
			
			if(mYear>maxYear){
				year.scroll(maxYear-mYear, 0);
			}
			
			
			if(mYear == maxYear && mMonth> maxMonth){
				month.scroll(maxMonth-mMonth, 0);
				month.invalidate();
			}
			
			
			if(mYear == maxYear&& mMonth== maxMonth&&mDay> maxDay){
				day.scroll(maxDay-mDay, 0);
				day.invalidate();
			}
			
		}
		
		if(isScrollBeforeToday){
			APPLog.i("zbkc", "yearmonthdialog:isScrollxx:"+isScroll+"mYear:"+mYear+"curYear:"+curYear);
			if(mYear<curYear){
				year.scroll(curYear-mYear, 0);
			}
			APPLog.i("zbkc", "yearmonthdialog:isScrollxx:"+isScroll+"mMonth:"+mMonth+"curMonth:"+curMonth);
			if(mYear<=curYear&&mMonth<(curMonth+1)){
				month.scroll(curMonth-mMonth+1, 0);
			}
			APPLog.i("zbkc", "yearmonthdialog:isScrollxx:"+isScroll+"mDay:"+mDay+"curDay:"+curDay);
			if(mYear<=curYear&&mMonth<=(curMonth+1)&&mDay<curDay){
				day.scroll(curDay-mDay, 0);
			}
		}
		
		setBtn1Enable(true);
		setBtn2Enable(true);
	}


	@Override
	public View createContentView() {
		return contentView;
	}
	
	public interface OnScrollEndListener{
		void onScrollEnd(String result,boolean scrollState);
	}
	private OnScrollEndListener onScrollEndListener;
	public void setOnScrollEndListener(OnScrollEndListener onScrollEndListener) {
		this.onScrollEndListener = onScrollEndListener;
	}
	public OnScrollEndListener getOnScrollEndListener() {
		return onScrollEndListener;
	}
	
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param position 滚动的位置
	 */
	public void yearScroll(int position) {
		year.scroll(position, 0);
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param position 滚动的位置
	 */
	public void monthScroll(int position) {
		month.scroll(position, 0);
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param position 滚动的位置
	 */
	public void dayScroll(int position) {
		day.scroll(position, 0);
	}
}

